package com.joinlabs.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.joinlabs.entity.Comment;
import com.joinlabs.entity.User;
import com.joinlabs.entity.Weibo;
import com.joinlabs.service.IFindCommentByidService;
import com.joinlabs.service.IFindweiboDetialByidService;
import com.joinlabs.service.IUserService;

@Controller
@RequestMapping("/CommentPage")
@SessionAttributes(names= {"commentlist","weiboDetail","user","firstList"})
public class CommentAction {
	 @Resource
	   private IUserService userService;//用户
	 @Resource
	   private IFindCommentByidService findCommentService;//评论内容
	  @Resource 
	  private IFindweiboDetialByidService weiboDetialByidService;//找微博详情
	 @RequestMapping("/comment")
	 @ResponseBody
	 public Object comment(ModelMap map,String WBid) {
		
		 System.out.println("WBid:"+WBid);
		 List<Comment> commentlist=findCommentService.selectAllCommentByWBid(Long.parseLong(WBid));

		 List<Comment> firstList = new ArrayList<>();
		 
		 //list长度为3
		 //改造list,让长度为1，还有两个回复在 评论实体里面的 list中
		 for(int i = 0;i<commentlist.size();i++) {
			 
			 Comment comm = commentlist.get(i);
			 
			 if(comm.getCommentedId() == 0) {	 
				 //说明是对微博的评论
				 comm.setCommentlist(new ArrayList<>());
				 firstList.add(comm);
				 
			 }

		 }

		 for(int i = 0;i<commentlist.size();i++) {
			 
			 Comment comm2 = commentlist.get(i);
			 if(comm2.getCommentedId() != 0) {
				 
				 //对评论的评论
				 //到底是对哪个评论的评论
				 for(int j = 0;j<firstList.size();j++) {
					 if(firstList.get(j).getCommentId() == comm2.getCommentedId()) {
						 firstList.get(j).getCommentlist().add(comm2);
						 break;
					 }
				 }
			 }
			 
		 }
		 map.addAttribute("firstList",firstList);
		 map.addAttribute("commentlist",commentlist);
		 //通过传入id查询微博详情
		Weibo  weiboDetail =weiboDetialByidService.selectDetailMsg(Long.parseLong(WBid));
			map.addAttribute("weiboDetail",weiboDetail);
			for (Comment comment : firstList) {
				 System.out.println("commentlist皮鞥了；****************"+comment);
			}
		 //System.out.println("commentlist"+commentlist);
			return new String("success"); 
	 }
	 @RequestMapping("/savecomment")
	 @ResponseBody
	 public Object savecomment(ModelMap map,String content,String wbid,String commentedID) {
		    User user=(User) map.get("user");
		    Long userId=user.getUserId();
		Long commentedId=Long.parseLong(commentedID);
		Long WeiboId=Long.parseLong(wbid);
		 //分装评论实体
		Comment comment=new Comment();
		//comment.setCommentId((long) 0);
		comment.setWeiboId(WeiboId);
		comment.setLogId((long) 0);
		comment.setUserId(userId);
		comment.setCommentContent(content);
		comment.setCommentTime(new Date());
		comment.setCommentZannum((long) 0);
		comment.setCommentedId(commentedId);
		//System.out.println(comment);
		 
	int row=findCommentService.insertComment(comment);
		 return comment;
	 }
	
	@RequestMapping("/showComment")
	 public String showComment() {
		 return "/hotPage/comment";
	 }
	 
}
